import {
    LOGIN,
    GETUSERINFO,
    UPDATASK,
    GETTEACHINGMATERIAL,
    MYCLASS,
    XUANBAN,
    CONFIRMTEXTBOOK,
    CHOOSEJIAOCAI,
    CHOOSEZHANGJIE,
    CHOOSEKESHI,
    TOUCHMOVE
} from './types'

import getters from './getters'
import publicFUN from '../public'


import Vue from 'Vue'
// import ElementUI from 'element-ui'
// import 'element-ui/lib/theme-default/index.css'
// Vue.use(ElementUI)

import VueRouter from 'vue-router'
// Vue.use(VueRouter)
let router = new VueRouter()


// 引入 axios
import axios from 'axios'


Vue.prototype.$http = axios

var state = {
    BeginTeaching: false,
    sendResourceOverDM:false,//推送资源后等待学生提交界面
    loginName: 'ddkjs001',
    TeachMaterial:false,//上课后素材资源弹出层
    loginPassWord: '123456',
    loading: true, //加载框
    picSrc: '', //头像路径地址
    resourceURL: '', //资源路径地址
    USERINFO: {},
    userType: '老师',
    enterClass: '未选择',
    enterClassID: '', //选择班级ID信息
    getToken: '', //请求sk
    teachingmaterial: [], //教材信息
    myClass: [], //班级信息
    isENTER: -1, //选择的班级
    isTEXTBOOK: -1, //选择的教材
    confirmTextBookID: '', //选择的教材ID
    confirmZHANGJIEID: '', //选择的章节ID
    configJIAOCAI: {
        JIAOCAI: '', //教材章节课时联动选择
        ZHANGJIE: '单击选择章节',
        KESHI: '单击选择课时'
    },
    showJIAOCAI: { //选择教材 章节 课时表显示与关闭
        JIAOCAI: false,
        ZHANGJIE: false,
        KESHI: false
    },
    infoZHANGJIE: [], //具体章节信息
    infoKESHI: [], //具体课时信息
    infoStudent: [], //班级学生具体信息
    infoStuPos: {}, //班级学生座位序号信息
    showStuIcon: false, //显示学生头像
    infoStudentGrounp: [], //学生分组
    lessionSteps: [], //教学资源步骤
    resource: [], //教学资源步骤信息
    NoKeshiData: false,//是否有资源
    showResource:false,//上课界面是否打开资源弹窗
    divContentTop: '280px', //题型下拉显示更多的值
    dialogClassTestisible: {
        show: false
    },//自学任务单弹窗
    LessonID:0,
    ResourceID:0,
    dialogSHITIVisible: {
        show: false
    },//试题弹窗
    ReTeachRecord:{//教师上课课堂记录表
        "ID": 0,
        "TeacherID": 0,
        "LessionID": 0,
        "ClassID": 0,
        "StartTime": "",
        "EndTime": "",
        "Status": 0,
        "InputTime": ""
    }
};


var mutations = {
    //题型下拉显示更多
    [TOUCHMOVE](state, event) {
        if (event.type == 'touchstart') {
            var startY = event.touches[0].clientY
            var startTop = state.divContentTop
            $(document).on('touchmove', function (e) {
                var endY = e.originalEvent.targetTouches[0].clientY
                var dy = endY - startY
                state.divContentTop = dy + parseFloat(startTop) + 'px';
                if (parseInt(state.divContentTop) <= 80) {
                    state.divContentTop = 80 + 'px'
                }
                if (parseInt(state.divContentTop) > parseFloat($('.divContent').height()) - 80) {
                    state.divContentTop = parseFloat($('.divContent').height()) - 80+'px'
                }
            })
            $(document).on('touchend', function () {
                $(document).unbind()
            })
        }
        if (event.type == 'mousedown') {
            var startY = event.clientY
            var startTop = state.divContentTop
            $(document).on('mousemove', function (e) {
                var endY = e.clientY
                var dy = endY - startY
                state.divContentTop = dy + parseFloat(startTop) + 'px';
                if (parseInt(state.divContentTop) <= 80) {
                    state.divContentTop = 80 + 'px'
                }
                if (parseInt(state.divContentTop) > parseFloat($('.divContent').height()) - 80) {
                    state.divContentTop = parseFloat($('.divContent').height()) - 80+'px'
                }
            })
            $(document).on('mouseup', function () {
                $(document).unbind()
            })
        }
    },
    [LOGIN](state) {
        /*Vue.prototype.$http.post('api/userinfo/login', {
         username: state.loginName,
         password: state.loginPassWord
         }).then(function (res) {
         state.getToken = res.data.data.sk
         document.cookie = 'getToken = ' + res.data.data.sk + ''

         if (res.data.message === '登陆成功！') {
         if (document.getElementsByClassName('popModal').length >= 1) {
         return
         }
         // Vue.prototype.$notify({
         //     title: '登陆成功',
         //     message: '登陆成功...',
         //     type: 'success',
         //     customClass: 'popModal',
         //     duration: '1200'
         // });
         console.log()
         setTimeout(function () {
         //登陆成功后获取用户信息
         Vue.prototype.$http.get(('api/userinfo/current?sk=' + state.getToken)).then(function (res) {
         state.USERINFO = res.data.data
         })
         //登陆成功后获取班级信息
         Vue.prototype.$http.get(('api/classinfo/myclass?sk=' + state.getToken)).then(function (res) {
         state.myClass = res.data.data
         })
         //登陆成功后获取当前老师教材
         Vue.prototype.$http.get(('api/teachingmaterial/myine?sk=' + state.getToken)).then(function (res) {
         state.teachingmaterial = res.data.data
         setTimeout(function () {
         document.querySelector('.TextBookList ul').getElementsByTagName('li')[0].setAttribute('class', 'confirmTextBook');
         state.configJIAOCAI.JIAOCAI = document.querySelector('.TextBookList ul').getElementsByTagName('li')[0].innerHTML;
         }, 100)
         })

         setInterval(function () {
         //每8分钟更新一次sk
         Vue.prototype.$http.get('api/auth/update').then(function (res) {
         document.cookie = 'getToken = ' + res.data.data + ''
         state.getToken = publicFun.getCookie('getToken')
         })
         }, 480000)

         router.push('/home/XB')
         }, 100)
         } else {
         if (document.getElementsByClassName('popModal').length >= 1) {
         return
         }
         // Vue.prototype.$notify({
         //     title: '登陆失败',
         //     message: '账号或密码错误...',
         //     type: 'error',
         //     customClass: 'popModal',
         //     duration: '1200'
         // });
         }

         })*/
    },
    [GETUSERINFO](state) {

    },
    [UPDATASK](state) {

    },
    //获取教材
    [GETTEACHINGMATERIAL](state) {

    },
    //获取教室信息
    [MYCLASS](state) {
        // Vue.prototype.$http.get(('api/classinfo/myclass?sk='+state.getToken)).then(function (res) {
        //     state.myClass = res.data.data
        // })
    },
    //选择班级方法
    // [XUANBAN](state, event){
    //     state.enterClassID = event.target.getAttribute('data-ID')
    //     state.enterClass = event.target.querySelector('.banji').innerHTML
    //     state.isENTER = event.target.getAttribute('data-index')
    // },
    //选择教材
    [CHOOSEJIAOCAI](state) {
        $('.layui-layer.layui-layer-tips.layer-anim').remove()
        for (let i in state.showJIAOCAI) {
            state.showJIAOCAI[i] = false
        }
        state.showJIAOCAI.JIAOCAI = true
    },
    //根据教材编号获取章节信息
    [CHOOSEZHANGJIE](state) {
        /*for (let i in state.showJIAOCAI) {
         state.showJIAOCAI[i] = false
         }

         if (state.confirmTextBookID != '') {
         let jiaocaiID = state.confirmTextBookID

         Vue.prototype.$http.get(('api/Chapter/find?id=' + jiaocaiID + '&sk=' + state.getToken)).then(function (res) {
         state.infoZHANGJIE = publicFUN.BuildTree(res.data.data, 0)
         })

         state.showJIAOCAI.ZHANGJIE = true
         } else {
         Vue.prototype.$notify({
         title: '操作失败',
         message: '请先选择教材...',
         type: 'error',
         customClass: 'popModal',
         duration: '1200'
         });
         return
         }*/

    },
    //根据章节编号获取课时信息
    [CHOOSEKESHI](state) {
        /*for (let i in state.showJIAOCAI) {
         state.showJIAOCAI[i] = false
         }
         if (state.confirmZHANGJIEID != '') {
         Vue.prototype.$http.get(('api/lession/period?id=' + state.confirmZHANGJIEID + '&sk=' + state.getToken)).then(function (res) {
         state.infoKESHI = res.data.data
         })
         } else {
         Vue.prototype.$notify({
         title: '操作失败',
         message: '请先选择章节...',
         type: 'error',
         customClass: 'popModal',
         duration: '1200'
         });
         return
         }
         state.showJIAOCAI.KESHI = true*/
    },
    //选择教材方法
    [CONFIRMTEXTBOOK](state, event) {
        $('.layui-layer.layui-layer-tips.layer-anim').remove()
        const _this = event.target
        document.querySelector('.confirmTextBook').setAttribute('class', '');
        state.confirmTextBookID = _this.getAttribute('data-ID');
        state.isTEXTBOOK = _this.getAttribute('data-index');
        state.configJIAOCAI.JIAOCAI = _this.innerHTML
        for (let i in state.showJIAOCAI) {
            state.showJIAOCAI[i] = false
        }
    },
    upLoginName(state, value) {
        state.loginName = value
    },
    upLoginPassWord(state, value) {
        state.loginPassWord = value
    },
    upinfoStudent(state, val) { //更新保存座位排序信息
        state.infoStudent = val
    },
    upinfoStudentGrounp(state, val) { //更新保存分组信息
        state.infoStudentGrounp = val
    }
};

export default {
    state,
    mutations,
    getters
}